﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;

public class MapManager : Singleton<MapManager> {

	public List<Map> _mapList;
	public int layerIndex;
	public Action OnSelectLayer;

	public MapManager(){
		_mapList = LoadMap();
		if (_mapList == null)
			_mapList = new List<Map>();
	}
	public int MapCount{
		get {return _mapList.Count;}
	}
	public void InsertMap(Map map,int index){
		if (index > _mapList.Count)
			_mapList.Add(map);
		else
			_mapList.Insert(index,map);
	}

	public void ReplaceMap(Map map,int index){
		if (index > _mapList.Count || index < 0) return;
        RemoveMap(index);
        InsertMap(map,index);
	}

	public void RemoveMap(int index){
		if (index < 0 || index >= _mapList.Count) return;
		_mapList.RemoveAt(index);
	}

	public bool SaveMap(){
		PlayerPrefs.SetInt("layerIndex",layerIndex);
		Utils.SaveByJson<List<Map>>(Application.dataPath,"mapList.json",_mapList);
		return true;
	}

	private List<Map> LoadMap(){
        return Utils.LoadByJson<List<Map>>(Application.dataPath,"mapList.json");
	}

	public Map GetMap(int index){
		if (index < 0 || index > _mapList.Count) return new Map();
		return _mapList[index];
	}
    public int GetDataCount(GMConfig.ProductType productType) {
        int Count = 0;
        switch (productType)
        {
            case GMConfig.ProductType.Card:
                Count = Utils.LoadByJson<List<Card>>(Application.dataPath, "cardList.json").Count;
                break;    
            case GMConfig.ProductType.InsertRoom:
            case GMConfig.ProductType.Event:
                Count = Utils.LoadByJson<List<Card>>(Application.dataPath, "fixedRoomList.json").Count;
                break;
            case GMConfig.ProductType.Boss:
                Count = BiologyMO.GetListOfType(GMConfig.BiologyType.Boss).Count;
                break;
            case GMConfig.ProductType.BossRetinue:
            case GMConfig.ProductType.EliteRetinue:
            case GMConfig.ProductType.Monster:
                Count = BiologyMO.GetListOfType(GMConfig.BiologyType.Normal).Count;
                break;
            case GMConfig.ProductType.Elite:
                Count = BiologyMO.GetListOfType(GMConfig.BiologyType.Elites).Count;
                break;
            case GMConfig.ProductType.BossRoom:
            case GMConfig.ProductType.EliteRoom:
            case GMConfig.ProductType.MonsterRoom:
                Count = Utils.GetFiles(Application.dataPath + "/Resources/Map/Blocks","*.prefab").Length;
                break;
        }
        return Count;
    }

    // void Get

    public string GetTitle(GMConfig.ProductType productType){
        string title = "";
        switch (productType)
        {
            case GMConfig.ProductType.Card:
                title = "卡牌模板列表";
                break;
            case GMConfig.ProductType.InsertRoom:
                title = "房間模板列表";
                break;
            case GMConfig.ProductType.Event:
                title = "事件模板列表";
                break;
            case GMConfig.ProductType.Boss:
                title = "Boss模板列表";
                break;
            case GMConfig.ProductType.BossRetinue:
                title = "Boss随从模板列表";
                break;
            case GMConfig.ProductType.Elite:
                title = "精英模板列表";
                break;
            case GMConfig.ProductType.EliteRetinue:
                title = "精英随从模板列表";
                break;
            case GMConfig.ProductType.Monster:
                title = "普怪模板列表";
                break;
            case GMConfig.ProductType.BossRoom:
            case GMConfig.ProductType.EliteRoom:
            case GMConfig.ProductType.MonsterRoom:
                title = "地块模板列表";
                break;
        }
        return title;
    }

	public int GetLayerIndex(){
		return PlayerPrefs.GetInt("layerIndex");
	}
	//获取房间指定键值对列表
	public KeyValueList GetKeyValueList(GMConfig.ProductType _productType){
        Map map = _mapList[layerIndex];
		for (int i = 0; i < map.keyValueList.Count; i++)
		{
			if (map.keyValueList[i].productType == _productType){
				return map.keyValueList[i];
			}
		}
		return null;
	}

    //获取房间指定键值对列表
    public int GetKeyValueCount(Map map, GMConfig.ProductType _productType)
    {
        // Map map = _mapList[layerIndex];
        for (int i = 0; i < map.keyValueList.Count; i++)
        {
            if (map.keyValueList[i].productType == _productType)
            {
                return map.keyValueList[i].list.Count;
            }
        }
        return 0;
    }

    //获取房间指定键值对列表-项
    public KeyValue GetKeyValue(Map map, GMConfig.ProductType _productType,int index)
    {
        for (int i = 0; i < map.keyValueList.Count; i++)
        {
            if (map.keyValueList[i].productType == _productType)
            {
                return map.keyValueList[i].list[index];
            }
        }
        return null;
    }

    //房间指定键值对列表-增加项
    public void AddKeyValue(Map map, GMConfig.ProductType _productType, KeyValue _keyValue){
        for (int i = 0; i < map.keyValueList.Count; i++)
        {
            if (map.keyValueList[i].productType == _productType)
            {
                Debug.Log("添加成功");
                map.keyValueList[i].list.Add(_keyValue);
            }
        }
	}

    //房间指定键值对列表-替换项
    public void ReplaceKeyValue(Map map, GMConfig.ProductType _productType,int target, KeyValue _keyValue)
    {
        for (int i = 0; i < map.keyValueList.Count; i++)
        {
            if (map.keyValueList[i].productType == _productType)
            {
                map.keyValueList[i].list[target] = _keyValue;
            }
        }
    }

    //房间指定键值对列表-范围增加项
    public void AddRangeKeyValue(Map map, GMConfig.ProductType _productType, List<KeyValue> list)
    {
        for (int i = 0; i < map.keyValueList.Count; i++)
        {
            if (map.keyValueList[i].productType == _productType)
            {
                map.keyValueList[i].list.AddRange(list);
            }
        }
    }

    //房间指定键值对列表-删除项
    public void RemoveKeyValue(Map map, GMConfig.ProductType _productType, int index)
    {
        for (int i = 0; i < map.keyValueList.Count; i++)
        {
            if (map.keyValueList[i].productType == _productType)
            {
                map.keyValueList[i].list.RemoveAt(index);
            }
        }
    }
    //替换地图内物品数量配置
    public void ReplaceCountRange(Map map, GMConfig.CountRange countRange, ProductCount productCount){
        map.pCountlist[(int)countRange].productCount = productCount;
    }
    //获取地图内物品数量配置
    public ProductCount GetCountRange(Map map, GMConfig.CountRange countRange)
    {
        return map.pCountlist[(int)countRange].productCount;

    }
    public override void Dispose(){
		base.Dispose();
		_mapList.Clear();
	}
}
